//
//  LoginCompanysViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/8/10.
//  Copyright © 2016年 RuiBen. All rights reserved.
//


#import "LoginCompanysViewController.h"
#import "MJRefresh.h"
#import "LoginCompanysViewCell.h"
//#import "TheLoginViewController.h"
#import "LoginCompanysModel.h"
#import "LoginViewController.h"


@interface LoginCompanysViewController ()<MBProgressHUDDelegate, UIGestureRecognizerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UISearchBarDelegate, LoginCompanysViewCellDelegate>

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, weak) UITableView *table;
@property (nonatomic, weak) UIView *noDataView;
@property (nonatomic, weak) UILabel *totalcount;
@property (nonatomic, strong) NSString*TotalCount;
@end
@implementation LoginCompanysViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

//- (void)viewDidDisappear:(BOOL)animated
//{
//    [super viewDidDisappear:animated];
//    self.navigationController.delegate = nil;
//}
//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//    }
//}


-(void)update:(NSString *)Str{
//    [self setNavigation];
    [self requestData];
    self.table.frame=CGRectMake(0, 0, ScreenWidth, ScreenHeight-TabbarSafeBottomMargin);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置tableView
    [self setUpTableView];
    
    [self requestData];
    
    self.modelArray=[[NSMutableArray alloc] init];
    // 设置导航条title字体大小
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontOfSize:17]};
    self.title = @"所属品牌";
 
}

- (void)setUpTableView{
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    self.table = table;
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.table registerClass:[LoginCompanysViewCell class] forCellReuseIdentifier:@"LoginCompanysViewController"];
    [self.view addSubview:self.table];
}



- (void)switchStores:(NSString *)sender{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:sender forKey:@"sgId"];
//    NSLog(@"-------%@",params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData1:@"Common/ChangeLoginGroup" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSLog(@"switchStores-------%@",severDataDict.dataDict);
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent0"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent1"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent2"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent3"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent4"];
                
                NSLog(@"----%@", [severDataDict.dataDict objectForKey:@"menus"]);
                
                
                [user setObject:[severDataDict.dataDict objectForKey:@"funs" ] forKey:@"funs"];
                [user setObject:[severDataDict.dataDict objectForKey:@"menus"] forKey:@"menus"];
                [user setObject:[severDataDict.dataDict objectForKey:@"tokenId"] forKey:@"tokenId"];
                [user setObject:[severDataDict.dataDict  objectForKey:@"funConfigs"] forKey:@"funConfigs"];
                [user synchronize];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTokenId" object:nil];
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
               
            }else{
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                
            }
        }
        else
        {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
            [MBProgressHUD showTextHudSub:subStr view:self.view];
        }
        
    }];
}
-(void)requestData{
    [self.totalcount removeFromSuperview];
    [self.noDataView removeFromSuperview];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    
    [HttpRequest requestData1:@"Common/LoginGroups" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            [self.modelArray removeAllObjects];
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                
                NSString *totalCount= [NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"TotalCount"]];
                self.TotalCount = totalCount;
                
                
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                if (mItemsArray.count>0) {
                    for (NSDictionary *dic in mItemsArray) {
//                        LoginCompanysModel *dataModel=[LoginCompanysModel dataModelWithDict:dic];
                        LoginCompanysModel *dataModel=[LoginCompanysModel mj_objectWithKeyValues:dic];
                        [self.modelArray addObject:dataModel];
                    }
                }else{
                    
                    UIView *noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 80)];
                    self.noDataView = noDataView;
                    [self.view addSubview:noDataView];
                    UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-25, 0, 50, 50)];
                    noImage.image=[UIImage imageNamed:@"meiyoushuju"];
                    [noDataView addSubview:noImage];
                    UILabel*noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,65, ScreenWidth, 14)];
                    noLabel.text=@"没有列表数据！";
                    noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:14];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [noDataView addSubview:noLabel];
                }
                
                [self.table reloadData];
            }else{
                [MBProgressHUD hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController*detailViewController = [[LoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:subStr view:self.view];
                
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
            [MBProgressHUD showTextHudSub:subStr view:self.view];
        }
        
    }];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoginCompanysViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginCompanysViewController"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    for (UIView *view in [cell subviews]){
        [view removeFromSuperview];
    }
       
    cell.model=self.modelArray[indexPath.row];
    cell.mDelegate=self;
    
    
    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  100;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
